import {createRouter, createWebHistory} from "vue-router";

const routerHistory = createWebHistory();
import {useAuthStore} from '../store/auth.js';
import pinia from "../store/store.js";

const authStore = useAuthStore(pinia);
const router = createRouter({
    history: routerHistory,
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('../view/index.vue'),
        },
        {
            path: '/console',
            name: 'console',
            component: () => import('../view/console.vue'),
            meta: {requiresAuth: true}
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('../view/register.vue')
        },
    ]
})
router.beforeEach((to, _, next) => {
    if (to.meta.requiresAuth && !authStore.token) {
        next({
            path: '/login',
            query: {redirect: to.fullPath}
        });
    } else {
        next();
    }
})
export default router;